package cn.tedu.ivos.approval.service.impl;

import cn.tedu.ivos.approval.mapper.ApplicationMapper;
import cn.tedu.ivos.approval.mapper.AuditMapper;
import cn.tedu.ivos.approval.pojo.dto.AuditQueryParam;
import cn.tedu.ivos.approval.pojo.dto.AuditSaveParam;
import cn.tedu.ivos.approval.pojo.entity.Application;
import cn.tedu.ivos.approval.pojo.entity.Audit;
import cn.tedu.ivos.approval.pojo.vo.AuditVO;
import cn.tedu.ivos.approval.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class AuditServiceImpl implements AuditService {

    @Autowired
    private AuditMapper auditMapper;

    @Autowired
    private ApplicationMapper applicationMapper;
    @Override
    public List<AuditVO> selectAudits(AuditQueryParam auditQueryParam) {
        return auditMapper.selectAudits(auditQueryParam);
    }

    @Override
    public void updateAudit(AuditSaveParam auditSaveParam) {//tom,mike,...
        Audit audit=new Audit();
        BeanUtils.copyProperties(auditSaveParam,audit);
        audit.setUpdateTime(new Date());
        Application application = new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
        //更新审批单状态
        if(audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //1.更新当前用户的申请状态
            auditMapper.updateAudits(audit);
            //2.更新同一个申请单对应的其它用户的审批单的审批状态。（默认初始值都是待审核）
            int count=auditMapper.selectRestAuditCount(audit.getApplicationId());
            System.out.println("count="+count);
            if(count>0){//count大于0表示还需要有人继续审批
                AuditQueryParam queryParam=new AuditQueryParam();
                queryParam.setAuditSort(audit.getAuditSort()+1);
                queryParam.setApplicationId(audit.getApplicationId());
                List<AuditVO> list = auditMapper.selectAudits(queryParam);
                System.out.println("list="+list);
                if(list!=null&&list.size()>0){
                    AuditVO auditVO=list.get(0);
                    audit=new Audit();
                    audit.setId(auditVO.getId());
                    audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    auditMapper.updateAudits(audit);
                }
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.update(application);
            }else{
              //修改申请单的状态
              application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
              applicationMapper.update(application);
            }
        }else if(audit.getAuditStatus().equals(AuditStatusEnum.REJECT.getCode())){
            System.out.println("audit:"+audit);
            //更新当前用户的审批单对应的审批状态
            auditMapper.updateAudits(audit);
            //更新同一个申请单对应的其它用户的审批单的审批状态(删除其它用户这个申请单对应审批单)
            AuditQueryParam auditQueryParam=new AuditQueryParam();
            auditQueryParam.setApplicationId(audit.getApplicationId());
            List<AuditVO> list = auditMapper.selectAudits(auditQueryParam);
            System.out.println("list="+list);
            for(AuditVO auditVO:list){
                 if(AuditStatusEnum.PENDING.getCode().equals(auditVO.getAuditStatus())){
                     System.out.println("delete:"+auditVO.getId());
                     auditMapper.deleteById(auditVO.getId());
                 }
            }
            //修改申请单的状态,并且给出驳回原因
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveParam.getRejectReason());
            applicationMapper.update(application);
        }
    }
}
